import Vue from "vue";
import { Module } from "vuex";
import { MqttData } from "../websocket/stores/nest";
import { RootState } from "../websocket/utils/store.type";

export type NestInfoError = {
	code: string;
	created_time: string;
	id: string;
	is_ignore: boolean;
	is_resolve: boolean;
	log: string;
	nest_id: string;
	p_code: string;
	resolve_result: string;
	resolve_time: string;
	solution: string;
};

export type NestInfo = {
	aircraftVideoUrl: string;
	aircraftVideoState: boolean;
	airportVideoUrl: string;
	airportVideoState: boolean;
	error: NestInfoError[];
	id: string;
	nestName: string;
	mqtt: MqttData;
	online: boolean;
	malfunction: boolean;
	// 控制开关id
	basalTrough: string;
	// 控制开关的密码
	// electricPassword:string;
	// 控制开关状态
	switchStatus: string;
	aircraftVideoPushURL: string;
	type: string; // 是否常绿
	controllerSite: string; // 跳转的地址
	timestamp: number;
	locationLatitude: string;
	locationLongitude: string;
};

export type GvaNestInfo = {
	ID: number;
	nestid: string;
	nestName: string;
	nestLocation: string;
	aircraftVideoPushURL: string;
	aircraftVideoURL: string;
	nestVideoURL: string;
};

export const state = {
	nestInfo: [] as NestInfo[],
	gvaNestInfoList: [] as GvaNestInfo[],
	tips: false
};

type State = typeof state;

const smartFlightState: Module<State, RootState> = {
	namespaced: true,

	state,

	mutations: {
		setGvaNestInfoList(state: State, gvaNestInfoList: GvaNestInfo[]): void {
			state.gvaNestInfoList = [...gvaNestInfoList];
		},
		setInfo(state: State, payload: NestInfo[]): void {
			state.nestInfo = [...payload];
		},
		setAircraftVideoState(state: State, payload: { id: string; state: boolean }): void {
			const index = state.nestInfo.findIndex((v: any) => v.id === payload.id);
			if (index !== -1) {
				Vue.set(state.nestInfo[index], "aircraftVideoState", payload.state);
			}
		},
		setTips(state: State, payload: boolean): void {
			state.tips = payload;
		},
		setSwitchStatusState(state: State, payload: { id: string; state: string }): void {
			const index = state.nestInfo.findIndex((v: any) => v.id === payload.id);
			if (index !== -1) {
				Vue.set(state.nestInfo[index], "switchStatus", payload.state);
			}
		}
	}
};

export default smartFlightState;
